package day16.feifei;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.URI;
import java.util.Arrays;
import java.util.List;

public class Demo1 implements Serializable {//Serializable标记接口

    //构造方法
    public static void m2() {
        //通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。如果给定字符串是空字符串，那么结果是空抽象路径名。
        File file = new File("D:\\file\\book");
        System.out.println(file);
        //
        String parentPath="D:\\aa\\bb\\";
        String childPath="cc.text";
        File file1=new File(parentPath,childPath);
        System.out.println(file1);
        File file2= new File("D:\\aa\\bb");
        File file3=new File(file2,childPath);
        System.out.println(file3);

        //URI
        // Create a URI object representing a file path
        URI uri = URI.create("file:///path/to/file.txt");
        // Create a File object using the URI
        File file4 = new File(uri);
        // Perform operations on the file
        System.out.println(file4);
        System.out.println("File exists: " + file4.exists());
        System.out.println("File name: " + file4.getName());
        System.out.println("File path: " + file4.getPath());
    }

    public static void m1() {
        String separator = File.separator;
        System.out.println(separator);//-------->windows \ ----->linux /
        String pathSeparator = File.pathSeparator;
        System.out.println(pathSeparator);//---->windows ;  --->linux :
    }
    public static void m3() throws IOException {
        File file = new File("D:\\file\\book\\c.txt");
        boolean newFile = file.createNewFile();
        System.out.println(newFile);
        File file2 = new File("D:\\file\\book\\cc");
        boolean mkdir = file2.mkdir();
        System.out.println(mkdir);
//生成多层目录
        File file3= new File("D:\\file\\book\\cc\\dd\\ee\\ff");
        boolean mkdirs = file3.mkdirs();
        System.out.println(mkdirs);

        //删除文件或文件夹
File file4=new File("D:\\file\\book\\cc\\dd\\ee\\ff");
        boolean delete = file4.delete();
        System.out.println(delete);

        File file5=new File("D:\\file\\book\\c.txt");
        boolean delete1 = file5.delete();
        System.out.println(delete1);

    }
    public static void m4(){
        File file=new File("D:\\file\\book\\cc\\dd\\ee\\ff.txt");
        String name = file.getName();
        System.out.println(name);
        System.out.println("-----------");
        //
        File absoluteFile = file.getAbsoluteFile();
        String parent = absoluteFile.getParent();
        System.out.println(absoluteFile);//D:\file\book\cc\dd\ee\ff.txt
        System.out.println(parent);//D:\file\book\cc\dd\ee
        System.out.println(absoluteFile.getParentFile());//D:\file\book\cc\dd\ee\
        String path="D:\\file\\book\\cc\\dd\\ee\\ff.txt";
        String[] split = path.split("\\\\");
        System.out.println(split[split.length-1]);
        //获取文件路径
        String path1 = file.getPath();
        System.out.println("path: "+path1);
        String absolutePath = file.getAbsolutePath();
        System.out.println("绝对路径"+absolutePath);

        File file2=new File("D:\\file\\book\\cc\\dd\\ee\\ff.txt");
        System.out.println(file2.getPath());
        System.out.println(file2.getAbsolutePath());

        boolean exists = file2.exists();
        System.out.println(exists);
    }

    //文件便利
    public static void m5(){
        //String[]
        File file=new File("D:\\file\\book");
        File[] list = file.listFiles();
        for (File file1 : list) {
            System.out.println(file1);
        }


    }
    public static void main(String[] args) throws IOException {
        m5();
    }
}
